package com.mycjj.android.fragment;

import java.util.List;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import com.mycjj.android.ProjectDataManage;
import com.mycjj.android.R;
import com.mycjj.android.activity.ServiceArticleDetailActivity;
import com.mycjj.android.adapter.MainArticlesAdapter;
import com.mycjj.android.bean.Point_StoreArticleRowBean;
import com.mycjj.android.bean.StoreArticleBaseBean;
import com.mycjj.android.http.ProjectHttpRequestImpl;
import com.mycjj.android.http.ProjectHttpRequestInterface;
import com.mycjj.android.pullrefresh.PullToRefreshBase;
import com.mycjj.android.pullrefresh.PullToRefreshBase.OnRefreshListener;
import com.mycjj.android.pullrefresh.PullToRefreshListView;
import com.mycjj.android.util.Preferences;

/**
 * 用品
 * @author mark
 *
 */
public class ServiceArticleFragment extends BaseFragment{
	public static ServiceArticleFragment newInstance() {
		ServiceArticleFragment fragment = new ServiceArticleFragment();
        return fragment;
    }
	private View rootView;
	private PullToRefreshListView mListView;
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		rootView = inflater.inflate(R.layout.fragment_main_new_car, null);
		mListView = (PullToRefreshListView)rootView.findViewById(R.id.lv_pull_refresh_list);
		mListView.setOnRefreshListener(new OnRefreshListener<ListView>() {
			@Override
			public void onRefresh(PullToRefreshBase<ListView> refreshView) {
				//请求数据
				mListView.onRefreshComplete();//归原来
			}
		});
		
		mListView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				Intent intent = new Intent(getActivity(),ServiceArticleDetailActivity.class);
				if(null != dataList)
				{
					intent.putExtra("article", dataList.get(position));
					startActivity(intent);
				}
			}
		});
		
		return rootView;
	}
	private MainArticlesAdapter adapter;
	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		initData();
	}
	
	List<Point_StoreArticleRowBean> dataList;
	private void initData(){
		Log.d("PointActivityFragment", "initData");
		adapter = new MainArticlesAdapter(getActivity());
		
		ProjectHttpRequestInterface request = new ProjectHttpRequestImpl(getActivity(), true);
		RequestParams params = new RequestParams();
		params.put("client_id", ProjectHttpRequestInterface.client_id_key);
		String storeid = Preferences.getSharedPreferences(getActivity(), "storeId", "");
		params.put("store_id", storeid);
		params.put("token",
				Preferences.getSharedPreferences(getActivity().getApplicationContext(), "user_token", ""));
		request.requestStoreGetStoreArticleWithDoneHandler("", params, new AsyncHttpResponseHandler() {
			@Override
			public void onSuccess(String content) {
				StoreArticleBaseBean articleBean = ProjectDataManage
						.getInstance().getStoreArticleBean();
				Log.d("PointActivityFragment", (null == articleBean)? "articleBean is null":"articleBean code"+ articleBean.getCode());
				if((null != articleBean) && (null != articleBean.getData()))
					{
						dataList = articleBean.getData().getList();
						adapter.setList(dataList);
					}
				}
			});
		
		
		mListView.setAdapter(adapter);
	}
}
